package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.utils.SpUtils;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.AccountDao;
import com.student.dms.databinding.ActivityLoginBinding;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {
    private ActivityLoginBinding activityLoginBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLoginBinding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(activityLoginBinding.getRoot());
        activityLoginBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        activityLoginBinding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegissterActivity.class));
            }
        });

        activityLoginBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editUsername = activityLoginBinding.editUsername.getText().toString().trim();
                String editPassword = activityLoginBinding.editPassword.getText().toString().trim();

                AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
                AccountDao accountDao = roomDatabase.provideAccountDao();
                Single<AccountEntity> accountFlow = accountDao.searchAccountByName(editUsername);

                disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                    //学生表不为空，查寝室表
                    if(!TextUtils.equals(accountEntity.getPassWord(),editPassword)){
                        ToastUtils.showToast(LoginActivity.this, "密码不正确请重新输入！");
                    }else{
                        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putString("username",editUsername);
                        edit.commit();
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        finish();
                    }
                }, studentFlowThrowable -> {
                    ToastUtils.showToast(LoginActivity.this, "用户不存在，请注册！");
                }));
            }
        });
    }
}
